package com.hptpd.questionnairesurvey.utils;

import com.google.common.collect.Lists;
import com.hptpd.questionnairesurvey.vo.DimensionalityVo;
import com.hptpd.questionnairesurvey.vo.QuestionVo;

import java.util.List;

/**
 * \* Created with IntelliJ IDEA.
 * \* Date: 2019/12/25
 * \*
 * \* Description:
 * \
 *
 * @author walter_long
 */
public class DimensionalityVoRecursionUtil {
    private List<QuestionVo> dest;
    private List<QuestionVo> dimensionalityQuestions;

    public DimensionalityVoRecursionUtil() {

    }

    private void RecursionGetQuestionList(DimensionalityVo dimensionalityVo) {
        if (null != dimensionalityVo.getQuestions() && !dimensionalityVo.getQuestions().isEmpty()) {
            dimensionalityQuestions.addAll(dimensionalityVo.getQuestions());
        }
        if (null == dimensionalityVo.getChildren() || dimensionalityVo.getChildren().isEmpty()) {
            return;
        } else {
            for (DimensionalityVo child : dimensionalityVo.getChildren()) {
                RecursionGetQuestionList(child);
            }
        }
    }

    public List<QuestionVo> filterNoDimensionalityQuestion(List<DimensionalityVo> dimensionalityVos, List<QuestionVo> questionVos) {
        dest = Lists.newLinkedList();
        dimensionalityQuestions = Lists.newLinkedList();
        dimensionalityVos.forEach(this::RecursionGetQuestionList);
        for (QuestionVo questionVo : questionVos) {
            boolean isExist = false;
            for (QuestionVo dimensionQuestionVo : dimensionalityQuestions) {
                if (null != questionVo.getId() && !questionVo.getId().isEmpty() && questionVo.getId().equals(dimensionQuestionVo.getId())) {
                    isExist = true;
                    break;
                } else if (null != questionVo.getDimensionTempTag() && !questionVo.getDimensionTempTag().isEmpty() && questionVo.getDimensionTempTag().equals(dimensionQuestionVo.getDimensionTempTag())){
                    isExist = true;
                    break;
                }
            }
            if (!isExist) {
                dest.add(questionVo);
            }
        }
        return dest;
    }
}
